package com.hpe.wx.performance.mongo.statusrecord.ctrl;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hpe.wx.performance.mongo.jmxObj.domain.JmxObj;
import com.hpe.wx.performance.mongo.jmxObj.service.JmxObjService;
import com.hpe.wx.performance.mongo.statusrecord.domain.StatusRecord;
import com.hpe.wx.performance.mongo.statusrecord.service.StatusRecordService;
import com.hpe.wx.performance.redis.utils.JMXUtils;

@Controller
@RequestMapping(value = "/hpe/performance/statusRecord")
public class StatusRecordCtrl {
	@Autowired
	private StatusRecordService service;

	@Autowired
	private JmxObjService service_jo;

	@ResponseBody
	@RequestMapping(value = "/findBy", method = RequestMethod.GET)
	public StatusRecord findBy(Locale locale, Model model, String jmxUrl) {
		if (null == jmxUrl) {
			jmxUrl = "service:jmx:rmi:///jndi/rmi://16.60.152.204:29602/jmxrmi";
		}
		List<StatusRecord> res = service.getAllRecord();
		System.out.println(res);
		JmxObj jk = service_jo.getByUrl(jmxUrl);

		res = service.getRecordBetween(1444810779239l, (new Date()).getTime());

		StatusRecord sr = service.getLastRecord(jk.getId());

		sr = service.getLastRecord(jk.getId());
		System.out.println(sr);
		return null;
	}

	@ResponseBody
	@RequestMapping(value = "/getLastRecord", method = RequestMethod.GET)
	public StatusRecord test(Locale locale, Model model, String jmxUrl, String id) {
		JmxObj jo = null;
		if (!StringUtils.isEmpty(jmxUrl)) {
			jo = service_jo.getByUrl(jmxUrl);
		} else {
			jo = service_jo.getById(id);
		}
		StatusRecord res = service.getLastRecord(jo.getId());
		return res;
	}

	@ResponseBody
	@RequestMapping(value = "/addARecord", method = RequestMethod.GET)
	public void addARecord(Locale locale, Model model, String jmxUrl) {
		JmxObj jot = service_jo.getByName("test");
		try {
			service.addARecord2DB(jot.getId());
		} catch (MalformedObjectNameException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AttributeNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstanceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MBeanException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ReflectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 主页
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String jmxUrlAdmin(Model model) {
		model.addAttribute("TestAttribute", "欢迎进入");
		List<StatusRecord> records = service.getAllRecord();
		model.addAttribute("jmxUrls", records);
		return "hpe/performancemongo/jmxUrlAdmin";
	}

	@ResponseBody
	@RequestMapping(value = "/checkJmxUrl", method = RequestMethod.GET)
	public Map<String, Object> checkJmxUrl(Locale locale, Model model, String jmxUrl) {
		jmxUrl = jmxUrl.trim();
		Map<String, Object> res = JMXUtils.checkJmxUrlUseable(jmxUrl);
		return res;
	}

	
	
	private List<StatusRecord> getAllRecord(Long startTimeStamp) {
		List<StatusRecord> res = service.getRecordAfter(startTimeStamp);

		return res;
	}

	private List<StatusRecord> getAllRecord(Long startTimeStamp, Long endTimeStamp) {
		List<StatusRecord> res = service.getRecordBetween(startTimeStamp, endTimeStamp);
		return res;
	}

}
